package com.cz.mes;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;

import java.io.File;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 更新预存数量
 */
public class UpdateProductionMaterialReservedQuantityFix {
    public static void main(String[] args) throws SQLException {
        String fileName = "UpdateReservedQuantity"+DateUtil.format(DateUtil.date(), "yyyy-MM-dd");
        File logFile = new File(fileName + ".log");

        List<String> logList = new ArrayList<>();

        String dbgroup = "mes-prod-master";     //mes-prod-master
        String billNo = "PBOMCZ2505190129";
        String skuCode = "YC20-75-50-20";
        BigDecimal updateReservedQuantity = new BigDecimal("50");


        String sql = "select id from production_material where bill_no = ?";
        Entity entity = Db.use(dbgroup).queryOne(sql, billNo);
        Integer productionMaterialId = entity.getInt("id");

        String sql1= "select id,item_sku_code,reserved_quantity,sent_quantity from production_material_detail where production_material_id = ? and item_sku_code=? and status=1";
        Entity productionMaterialDetailEntity = Db.use(dbgroup).queryOne(sql1, productionMaterialId,skuCode);
        String itemSkuCode = productionMaterialDetailEntity.getStr("item_sku_code");
        Integer reservedQuantity = productionMaterialDetailEntity.getInt("reserved_quantity");
        Integer sentQuantity = productionMaterialDetailEntity.getInt("sent_quantity");
        Integer productionMaterialDetailId = productionMaterialDetailEntity.getInt("id");
        String content ="productionMaterialDetailId: " + productionMaterialDetailId + " itemSkuCode: " + itemSkuCode + " reservedQuantity: " + reservedQuantity + " sentQuantity: " + sentQuantity;

        if(productionMaterialDetailId!= null){
            logList.add(content);
            String sql2 = "update production_material_detail set reserved_quantity = ? where id= ?";
            int result = Db.use(dbgroup).execute(sql2, updateReservedQuantity, productionMaterialDetailId);
            System.out.println("更新了"+result+"数据");
        }

        FileUtil.writeLines(logList, logFile, "utf-8",true);
    }
}
